package com.njtoyo.taxi.admin.service.marketing.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.mapper.master.entity.CouponPurchaseRule;
import com.njtoyo.taxi.admin.mapper.master.entity.GeoCustomerRegion;
import com.njtoyo.taxi.admin.mapper.master.mapper.CouponPurchaseRuleMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.GeoCustomerRegionMapper;
import com.njtoyo.taxi.admin.rest.wrapper.marketing.freeCommission.FreeCommissionCardAddWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.marketing.freeCommission.FreeCommissionCardQueryWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.marketing.freeCommission.FreeCommissionCardUpdateWrapper;
import com.njtoyo.taxi.admin.service.marketing.CouponPurchaseRuleService;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@DS(Const.PRIMARY_DS)
public class CouponPurchaseRuleServiceImpl extends ServiceImpl<CouponPurchaseRuleMapper, CouponPurchaseRule> implements CouponPurchaseRuleService {
    @Autowired
    private GeoCustomerRegionMapper geoCustomerRegionMapper;

    @Override
    public RestResult add(FreeCommissionCardAddWrapper wrapper) {
        CouponPurchaseRule freeCommissionCard = BeanUtil.copyProperties(wrapper, CouponPurchaseRule.class);
        baseMapper.insert(freeCommissionCard);
        return RestResult.success();
    }

    @Override
    public RestResult list(FreeCommissionCardQueryWrapper wrapper) {
        Page<CouponPurchaseRule> page = new Page<>(wrapper.getCurrent(), wrapper.getSize());
        LambdaQueryWrapper<CouponPurchaseRule> couponPurchaseRuleLambdaQueryWrapper = Wrappers.<CouponPurchaseRule>lambdaQuery();
        if (StrUtil.isNotBlank(wrapper.getTitle())) {
            couponPurchaseRuleLambdaQueryWrapper.eq(CouponPurchaseRule::getTitle, wrapper.getTitle());
        }
        if (ObjectUtil.isNotNull(wrapper.getIsActive())) {
            couponPurchaseRuleLambdaQueryWrapper.eq(CouponPurchaseRule::getIsActive, wrapper.getIsActive());
        }
        Page<CouponPurchaseRule> pageResult = baseMapper.selectPage(page, couponPurchaseRuleLambdaQueryWrapper);
        for (CouponPurchaseRule rule : pageResult.getRecords()) {
            GeoCustomerRegion geoCustomerRegion = geoCustomerRegionMapper.selectById(rule.getCustomerRegionId());
            if (ObjectUtil.isNotNull(geoCustomerRegion)) {
                rule.setCustomerRegion(geoCustomerRegion.getName());
            }
        }
        return RestResult.success(pageResult);
    }

    @Override
    public RestResult updateActive(FreeCommissionCardUpdateWrapper wrapper) {
        CouponPurchaseRule rule = new CouponPurchaseRule();
        rule.setId(wrapper.getId());
        rule.setIsActive(wrapper.getIsActive());
        baseMapper.updateById(rule);
        return RestResult.success();
    }

    @Override
    public RestResult update(FreeCommissionCardUpdateWrapper wrapper) {
        CouponPurchaseRule freeCommissionCard = BeanUtil.copyProperties(wrapper, CouponPurchaseRule.class);
        baseMapper.updateById(freeCommissionCard);
        return RestResult.success();
    }

    @Override
    public RestResult delete(Long id) {
        baseMapper.deleteById(id);
        return RestResult.success();
    }
}
